package com.zzzzzz.oms.orderHead;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.validation.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zzzzzz.oms.legs.Legs;
import com.zzzzzz.oms.legs.LegsDao;
import com.zzzzzz.oms.legs.LegsService;
import com.zzzzzz.oms.ordertype.OrderTypeDao;
import com.zzzzzz.oms.orderHead.OrderHead;
import com.zzzzzz.oms.tempShipment.TempShipment;
import com.zzzzzz.oms.tempShipment.TempShipmentDao;
import com.zzzzzz.plugins.shiro.I;
import com.zzzzzz.sys.client.ClientDao;
import com.zzzzzz.sys.client.ClientService;
import com.zzzzzz.sys.dict.DictDao;

@Service
public class OrderHeadService {
	
	@Resource
	public OrderHeadDao orderHeadDao;
	
	@Resource
	public Validator validator;
	
	@Resource
	public ClientDao clientDao;
	
	@Resource
	public OrderTypeDao orderTypeDao;
	
	@Resource
	public DictDao dictDao;
	
	@Resource
	public ClientService clientService;
	
	@Resource
	public LegsService legsService;
	
	@Resource
	public LegsDao legsDao;
	
	@Resource
	public TempShipmentDao tempShipmentDao;

	// 增加、修改
	@Transactional
	public Long save(OrderHead orderHead, I i) {
		orderHead.setUpdDt(new Date());
		orderHead.setUpdBy(i.getId());
		if (orderHead.getId() == null) {
			orderHead.setAddDt(new Date());
			orderHead.setAddBy(i.getId());
			orderHead.setPlatformId(i.getPlatformId());
			Long id = orderHeadDao.add(orderHead);
			return id;
		} else {
			orderHeadDao.updById(orderHead);
		}
		return (long) 1;
	}
	
	// 处理生效之后的数据
	public void arrived(List<OrderHead> list, List<Long> ids, List<String> listVocation, I i, String code){
		saveLegs(list, i);
		saveTemp(listVocation, i, code);
	}
	
	// 订单失效
	public void back(List<Long> ids, List<Long> tempShipmentIds, I i){
		legsDao.delByIds(ids);
		changeTemp(tempShipmentIds, i);
	}
	
	// 生成分段订单
	public void saveLegs(List<OrderHead> listOrder,I i){
		List<Legs> legsList = new ArrayList<Legs>();
		for(OrderHead orderHead:listOrder){
			Legs legs = legsService.add(orderHead, i);
			legsList.add(legs);
		}
		legsDao.saveOrder(legsList);
	}
	
	// 生成高级调度
	public void saveTemp(List<String> listVocation,I i,String code){
		List<TempShipment> list = tempShipmentDao.findByVocations(code, listVocation, i);
		List<Long> listIds = new ArrayList<Long>();
		for(TempShipment tempShipment:list){
			listIds.add(tempShipment.getId());
		}
		List<Legs> legs=legsDao.findGrouptempIds(listIds);
		for(Legs leg:legs){
			TempShipment tempShipment = make(leg, i);
			tempShipment.setId(leg.getTempshipmentId());
			tempShipment.setCount(legsDao.findCountByTempId(leg.getTempshipmentId()));
			tempShipmentDao.updById(tempShipment);
		}
	}
	
	// 订单失效
	public void changeTemp(List<Long> ids,I i){
		List<Legs> list = legsDao.findGrouptempIds(ids);	
		for(Long id:ids){
			Long id1 = (long) 0;
			Legs legs = null;
			for(Legs leg:list){
				if(id.equals(leg.getTempshipmentId())){
					legs = leg;
					id1 = id;
					break;
				}
			}
			if(id1>0){
				TempShipment tempShipment = make(legs, i);
				tempShipment.setId(id);
				tempShipment.setCount(legsDao.findCountByTempId(id));
				tempShipmentDao.updById(tempShipment);
			}else{
				TempShipment tempShipment = new TempShipment();
				tempShipment.setId(id);
				tempShipment.setQuantity(0.0);
				tempShipment.setWeight(0.0);
				tempShipment.setVolume(0.0);
				tempShipment.setPalletsum(0.0);
				tempShipment.setUpdBy(i.getId());
				tempShipment.setUpdDt(new Date());
				tempShipment.setPoints(0);
				tempShipment.setCount(0);
				tempShipmentDao.updById(tempShipment);
			}
		}
		for(Legs leg:list){
			if(leg.getQuantity() == null){
				leg.setQuantity(0.0);
			}
			if(leg.getWeight() == null){
				leg.setWeight(0.0);
			}
			if(leg.getVolume() == null){
				leg.setVolume(0.0);
			}
			if(leg.getPalletsum() == null){
				leg.setPalletsum(0.0);
			}
			TempShipment tempShipment = make(leg, i);
			tempShipment.setId(leg.getTempshipmentId());
			tempShipment.setCount(legsDao.findCountByTempId(leg.getTempshipmentId()));
			tempShipmentDao.updById(tempShipment);
		}
	}
	
	// 封装
	public TempShipment make(Legs leg, I i){
		TempShipment tempShipment = new TempShipment();
		tempShipment.setQuantity(leg.getQuantity());
		tempShipment.setWeight(leg.getWeight());
		tempShipment.setVolume(leg.getVolume());
		tempShipment.setPalletsum(leg.getPalletsum());
		tempShipment.setUpdBy(i.getId());
		tempShipment.setUpdDt(new Date());
		tempShipment.setPoints(leg.getPoints());
		return tempShipment;
	}
	private static final Logger logger = LoggerFactory.getLogger(OrderHead.class);
}
